from fastapi import APIRouter, UploadFile, HTTPException
from fastapi.responses import JSONResponse

from app.utils.file import save_upload_file

router = APIRouter()

@router.post("/image")
async def upload_image(file: UploadFile):
    """
    上传图片
    """
    try:
        # 1、保存文件
        file_url = await save_upload_file(file)
        
        # 2、返回响应
        return JSONResponse({
            "url": file_url,
            "message": "图片上传成功"
        })
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail="文件上传失败")